<%--
  Created by IntelliJ IDEA.
  User: xiheng
  Date: 2021/7/5
  Time: 17:17
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <script type="text/javascript">
    function test(){
      //发送异步请求
      //1.创建XMLHttpRequest对象
      //1.1 判断浏览器
      var xmlhttp;

      if (window.XMLHttpRequest){
          //非IE浏览器
          xmlhttp=new XMLHttpRequest();
      }else if (window.ActiveXObject){
          xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }

      //2.打开连接
      var uname = document.getElementById("uname").value;
      xmlhttp.open("get","/testuname?username="+uname,true);
      //post提交
        /*xmlhttp.open("post","/testuname",true);*/

      //3.指定回调函数
      xmlhttp.onreadystatechange=function () {
        //3.1 判断状态
        if (xmlhttp.readyState==4){
          //3.2 接收返回的数据
          var responseText = xmlhttp.responseText;
          document.getElementById("rs").innerText=responseText;
        }
      }
      //4.发送数据
      xmlhttp.send();
      //post提交
        /*xmlhttp.setRequestHeader("Content-type", "application/x-www-formurlencoded;charset=UTF-8");
        xmlhttp.send("username="+uname);*/
    }
  </script>
  <body>
      用户名：<input type="text" id="uname" onblur="test()">
            <span id="rs"></span>
  </body>
</html>
